package com.laomuji.android.mvvm.utils

import android.view.View

/**
 * 防止短时间重复点击
 * @author laomuji666
 * @since 2025/7/1
 */
object ClickDebounceUtil {
    private val CLICK_MAP = HashMap<Int, Long>()

    fun setClickListenerDebounce(view: View, listener: View.OnClickListener?) {
        view.setOnClickListener { v: View? ->
            val identityHashCode = System.identityHashCode(view)
            val lastTime = CLICK_MAP[identityHashCode]
            val currentTimeMillis = System.currentTimeMillis()
            if (lastTime == null || (currentTimeMillis - lastTime > 500)) {
                listener?.onClick(v)
                CLICK_MAP[identityHashCode] = currentTimeMillis
            }
        }
    }
}
